package com.fc.mall.service.impl;

import com.fc.mall.exception.ServiceException;
import com.fc.mall.mapper.PmsProductMapper;
import com.fc.mall.mapper.UmsMemberLevelMapper;
import com.fc.mall.mapper.UmsMemberMapper;
import com.fc.mall.model.*;
import com.fc.mall.model.PmsProduct;
import com.fc.mall.model.UmsMemberExample;
import com.fc.mall.model.UmsMemberLevel;
import com.fc.mall.model.UmsMemberLevelExample;
import com.fc.mall.service.UmsMemberLevelService;
import com.fc.mall.util.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 会员等级管理Service实现类
 * Created by dobuy on 2018/4/26.
 */
@Service
public class UmsMemberLevelServiceImpl implements UmsMemberLevelService{
    @Autowired
    private UmsMemberLevelMapper memberLevelMapper;

    @Autowired
    private UmsMemberMapper umsMemberMapper;

    @Autowired
    private PmsProductMapper productMapper;

    @Override
    public List<UmsMemberLevel> list() {
        UmsMemberLevelExample example = new UmsMemberLevelExample();
        example.setOrderByClause("sort asc");
        return memberLevelMapper.selectByExample(example);
    }

    @Override
    public UmsMemberLevel getItem(Long id) {
        UmsMemberLevel memberLevel = memberLevelMapper.selectByPrimaryKey(id);
        if (null != memberLevel.getProductId()){
            PmsProduct pmsProduct = productMapper.selectByPrimaryKey(memberLevel.getProductId());
            memberLevel.setProductName(pmsProduct.getName());
        }
        return memberLevel;
    }

    @Override
    public int delete(Long id) {
        return memberLevelMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int create(UmsMemberLevel umsMemberLevel) {
        // 新添加的等级定义，如果是新用户默认等级
        if(umsMemberLevel.getDefaultStatus() == 1)
        {
            UmsMemberLevelExample example = new UmsMemberLevelExample();
            example.createCriteria().andDefaultStatusEqualTo(1);
            List<UmsMemberLevel> levelDefinitionList = memberLevelMapper.selectByExample(example);
            if(levelDefinitionList != null && !levelDefinitionList.isEmpty())
            {
                throw new ServiceException("新用户默认等级已存在， 该类别等级只需要一个，请重新选择！");
            }
        }
        return memberLevelMapper.insert(umsMemberLevel);
    }

    @Override
    public int update(Long memberLevelId, UmsMemberLevel umsMemberLevel) {
        //若该状态存在用户，该状态不可关闭
        if (null != umsMemberLevel.getActive() &&
                umsMemberLevel.getActive().equals(Constants.Status.Inactive.getValue())){
            UmsMemberExample memberExample = new UmsMemberExample();
            memberExample.createCriteria().andMemberLevelIdEqualTo(memberLevelId);
            int memberNum = umsMemberMapper.countByExample(memberExample);
            if (memberNum > 0){
                throw new ServiceException("已有会员升级到该状态，当前状态不可关闭");
            }
        }

        // 新添加的等级定义，如果是新用户默认等级
        umsMemberLevel.setId(memberLevelId);
        return memberLevelMapper.updateByPrimaryKeySelective(umsMemberLevel);
    }
}
